package com.hn.common.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hn.framework.web.domain.AjaxResult;
import com.hn.upload.FileUploadUtil;
import com.hn.upload.UploadPlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * 描述:
 *
 * @date 2019-11-14 14:41
 */
public class CommonUtils {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    public static void responseResult(HttpServletResponse response, AjaxResult result) {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "application/json;charset=UTF-8");
        response.setStatus(200);
        try {
            response.getWriter().write(JSON.toJSONString(result));
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }



    public static String pingJieUrls(String urls,String webUrl) {
        if (StrUtil.isBlank(urls)) {
            return null;
        }

        String[] imgArr = urls.split(",");
        if (imgArr.length <= 0) {
            return null;
        }

        StringBuffer imgUrls = new StringBuffer();

        for (int i = 0; i < imgArr.length; i++) {
            String imgUrl = imgArr[i];
            if (i == imgArr.length - 1) {
                imgUrls.append(webUrl).append(imgUrl);
            } else {
                imgUrls.append(webUrl).append(imgUrl).append(",");
            }
        }

        return String.valueOf(imgUrls);
    }

    public static String pingJieUrl(String url,String webUrl) {
        if (StrUtil.isBlank(url)) {
            return null;
        }
        if (url.startsWith("http://") || url.startsWith("https://")) {
            return url;
        }
        return webUrl + url;
    }

    public static boolean contains(Integer id, String ids) {
        if (id == null || StrUtil.isBlank(ids)) {
            return false;
        }

        String[] split = ids.split(",");
        for (String s : split) {
            if (id.equals(Integer.valueOf(s))) {
                return true;
            }
        }
        return false;
    }


}
